package com.spzx.test.t13;

import lombok.SneakyThrows;
import org.apache.commons.lang3.RandomUtils;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

public class BreadRoom {

    BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(5);

    @SneakyThrows
    public void put(String bread){
        System.out.println(Thread.currentThread().getName()+"向货架上放入一块面包:"+bread);
        blockingQueue.put(bread);
        TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1,5));
        System.out.println(blockingQueue);
    }

    @SneakyThrows
    public String get(){
        String take = blockingQueue.take();
        System.out.println(Thread.currentThread().getName()+"拿走一块面包:"+take);
        TimeUnit.SECONDS.sleep(RandomUtils.nextInt(1,5));
        System.out.println(blockingQueue);
        return take;
    }


}
